package dobest.ray.com.jiongjiong.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dobest.ray.corelibs.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.bean.City;

/**
 * 热门城市适配器
 * 
 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
 *
 * @Date: 2015-4-9
 */
public class HotCitiesAdapter extends BaseAdapter {
	private int currentSelected = -1;

	private List<City> data = new ArrayList<City>();
	private Context context;

	public HotCitiesAdapter(Context context, List<City> data) {
		if (data != null) {
			this.data = data;
		}
		this.context = context;
	}

	public List<City> getData() {
		return data;
	}

	public void setData(List<City> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public City getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = LayoutInflater.from(context).inflate(R.layout.item_hot_city, null);

		City city = data.get(position);

		TextView name = ViewHolder.get(convertView, R.id.tv_hot_city);

		name.setText(city.name);
		if (currentSelected == position) {
			name.setTextColor(context.getResources().getColor(R.color.colorPrimary));
			name.setBackgroundResource(R.drawable.item_hot_city_bg);
		} else {
			name.setTextColor(context.getResources().getColor(R.color.dark));
			name.setBackgroundResource(R.drawable.item_hot_city_noselected_bg);
		}

		return convertView;
	}

	/**
	 * 设置选择后的背景
	 * 
	 * @param position
	 */
	public void setSelected(int position) {
		this.currentSelected = position;
		notifyDataSetChanged();
	}

	public void setRefreData(List<City> data) {
		this.data = data;
		notifyDataSetChanged();
	}

}
